﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using RadinfMagazynier3.DataSets;

namespace RadinfMagazynier3.Forms
{
    public partial class SettingsForm : DevExpress.XtraEditors.XtraForm
    {
        private MagazynierDS _dataSource;
        private DataSet _temDataSource;
        public MagazynierDS DataSource
        {
            get { return this._dataSource; }
            set { this._dataSource = value; }
        }

        #region CTOR
        public SettingsForm(MagazynierDS dataSource)
        {
            InitializeComponent();
            _dataSource = dataSource;
            _temDataSource = dataSource.Copy();
            prepareSettingsDataRow();
            prepareLookUps();
            setDataBinding();
        }

        private void setDataBinding()
        {
            this.teName.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "NAME"));
            this.teAddress.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "ADDRESS"));
            this.teCity.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "CITY"));
            this.teZIP.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "ZIP"));
            this.teNIP.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "NIP"));
            this.teRegon.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "REGON"));
            this.leDefVat.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "DEF_VAT"));
            this.leDefMeasure.DataBindings.Add(new Binding("EditValue", _temDataSource.Tables["SETTINGS"], "DEF_MEASURE"));

            this.gridControl1.DataMember = "VAT";
            this.gridControl1.DataSource = _temDataSource;

            this.gridControl2.DataMember = "MEASURE";
            this.gridControl2.DataSource = _temDataSource;
        }

        private void prepareSettingsDataRow()
        {
            if (_temDataSource.Tables["SETTINGS"].Rows.Count == 0)
            {
                DataRow dr = _temDataSource.Tables["SETTINGS"].NewRow();
                _temDataSource.Tables["SETTINGS"].Rows.Add(dr);
            }
        }

        private void prepareLookUps()
        {
            this.leDefMeasure.Properties.BeginInit();
            this.leDefMeasure.Properties.DataSource = MainForm.DataSource.MEASURE;
            this.leDefMeasure.Properties.DisplayMember = "DESC";
            this.leDefMeasure.Properties.ValueMember = "ID";
            this.leDefMeasure.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DESC", "Miara"));
            this.leDefMeasure.Properties.EndInit();

            this.leDefVat.Properties.BeginInit();
            this.leDefVat.Properties.DataSource = MainForm.DataSource.VAT;
            this.leDefVat.Properties.DisplayMember = "DESC";
            this.leDefVat.Properties.ValueMember = "ID";
            this.leDefVat.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DESC", "VAT"));
            this.leDefVat.Properties.EndInit();
        }
        #endregion

        #region EVENTS
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            _temDataSource.Tables["SETTINGS"].AcceptChanges();
            this._dataSource.Merge(this._temDataSource);
            this.DialogResult = DialogResult.OK;
        }
        #endregion
    }
}